package com.bridgeintelligent.tag.customer.controller;

import com.alibaba.fastjson.JSON;
import com.bridgeintelligent.tag.customer.entity.AnalysisSetup;
import com.bridgeintelligent.tag.customer.entity.Result;
import com.bridgeintelligent.tag.customer.service.IAnalysisSetupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "分析设置管理")
@AllArgsConstructor
@RestController
@RequestMapping("/api/setup")
@Slf4j
public class AnalysisSetupController {

    private IAnalysisSetupService setupService;

    @ApiOperation("新增分析设置")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "setup", value = "分析设置信息")
    })
    @PostMapping("/add")
    public Result addSetup(@RequestBody AnalysisSetup setup) {
        try {
            setupService.add(setup);
            return new Result(0,"保存成功");
        } catch (Exception e) {
            log.error("保存失败：{}::"+e);
            return new Result(-1,"保存失败");
        }
    }


    public static void main(String[] args) {
        List<Map<String,String>> list = new ArrayList<>();
        Map map = new HashMap();
        map.put("low","-3");
        map.put("high","1");
        list.add(map);
        System.out.println(JSON.toJSONString(list));
    }


    @ApiOperation("修改分析设置")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", name = "setup", value = "分析设置信息")
    })
    @PostMapping("/update")
    public Result updateSetup(@RequestBody AnalysisSetup setup) {
        try {
            setupService.update(setup);
            return new Result(0,"修改成功");
        } catch (Exception e) {
            log.error("修改失败：{}::"+e);
            return new Result(-1, "修改失败");
        }
    }

    @ApiOperation("根据ID删除分析设置")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "分析ID")})
    @GetMapping("/delById")
    public Result delById(String id) {
        try {
            setupService.delById(id);
            return new Result(0,"删除成功");
        }catch (Exception e){
            log.error("删除失败：{}::"+e);
            return new Result(-1,"删除失败");
        }

    }

    @ApiOperation("根据TpId删除分析设置")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "tpId", value = "模板ID")})
    @GetMapping("/delByTpId")
    public Result delByTpId(String tpId) {
        try {
            setupService.delByTpId(tpId);
            return new Result(0,"删除成功");
        }catch (Exception e){
            log.error("删除失败：{}::"+e);
            return new Result(-1,"删除失败");
        }

    }

    @ApiOperation("查看分析设置详情")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "分析ID")
    })
    @GetMapping("/findById")
    public Result findById(String id) {
        try {
            AnalysisSetup setup = setupService.findById(id);
            return new Result(0,"查询成功",setup);
        }catch (Exception e){
            log.error("查看详情失败：{}::"+e);
            return new Result(-1,"查看详情失败");
        }
    }

    @ApiOperation("根据模板ID查询分析设置列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "tpId", value = "模板ID")
    })
    @GetMapping("/findByTpId")
    public Result findByTpId(String tpId) {
        try {
            List<AnalysisSetup> list = setupService.findList(tpId);
            return new Result(0,"查询成功",list);
        }catch (Exception e){
            log.error("查询失败：{}::"+e);
            return new Result(-1,"查询失败");
        }
    }
}